<?php


namespace App\Http\Controllers\home;

use App\Http\Controllers\Controller;
use App\Models\Carts;
use App\Models\Users;
use Illuminate\Http\Request;

class CartsController extends Controller
{
    /**
     * 添加购物车或减少购物车数量
     * @param url home.cart.cartAddApi
     * @param Request $request
     * @param store_id 店铺id
     * @param store_name 店铺名称
     * @param goods_id 商品id
     * @param goods_name 商品名称
     * @param goods_img 商品图片
     * @param goods_price 商品价格
     * @param num 数量
     * @param type 类型 1.增加 2.减少
     * @return bool
     */
    public function cartAddApi(Request $request) {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $store_id = (int)$request->post("store_id");
            $store_name = (string)$request->post("store_name");
            $goods_id = (int)$request->post("goods_id");
            $goods_name = (string)$request->post("goods_name");
            $goods_img = (string)$request->post("goods_img");
            $goods_price = (string)$request->post("goods_price");
            $num = (int)$request->post("num");
            $type = (int)$request->post("type");//类型 1.增加 2.减少
            if (empty($store_id) || empty($store_name) || empty($goods_id) ||
                empty($goods_name) || empty($goods_img) || empty($goods_price) || empty($num) || empty($type)){
                return $this->writeJson(1,null,'参数错误');
            }
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            if ($userinfo != 1) return $this->writeJson(1,null,'供应商不能购买商品');

            //查询购物车是否已经存在同一用户 同一规格 同一商品
            $cart = Carts::where(['user_id' => $user_id,'goods_id' => $goods_id,'store_id' => $store_id])->first();
            if (!empty($cart)) {
                if ($type == 1) $cart->num = $cart["num"] + $num;
                if ($type == 2) {
                    if ($cart["num"] > 1) {
                        $cart->num = $cart["num"] - $num;
                    } else {
                        return $this->writeJson(1,null,'只有一个，不能再减了');
                    }
                }
            } else {
                if ($type == 1) {
                    $cart = new Carts();
                    $cart->user_id = $user_id;
                    $cart->store_id = $store_id;
                    $cart->store_name = $store_name;
                    $cart->goods_id = $goods_id;
                    $cart->goods_name = $goods_name;
                    $cart->goods_img = $goods_img;
                    $cart->goods_price = $goods_price;
                    $cart->num = $num;
                } else {
                    return $this->writeJson(1,null,'参数错误');
                }
            }
            $cart->save();
            return $this->writeJson(0, null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 购物车列表页面
     * @param url home.cart.cartList
     * @param Request $request
     * @return bool
     */
    public function cartList(){
        return view('home.cart.list');
    }
    /**
     * 购物车列表
     * @param url home.cart.cartListsApi
     * @param Request $request
     */
    public function cartListsApi(Request $request) {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
//            $page = (int)$request->get('page',1);//页码
//            if (empty($page)) return $this->writeJson(1,null,'参数错误');
            $userinfo = Users::where(['id'=>$user_id, 'status'=>1])->value('type');
            if (empty($userinfo)) return $this->writeJson(1,null,'用户不存在');
            $lists = Carts::allLists(['user_id'=>$user_id], ['*'], null, 200);
            if (!empty($lists)){
                //购物车商品以店铺ID分组显示
                foreach ($lists as $key => $data){
                    foreach ($data as $K=>$v){
                        $data['bool'] = false;
                    }
                    $store_cart_list[$data['store_id']]['bool'] = false;
                    $store_cart_list[$data['store_id']]['store_name'] = $data['store_name'];
                    $store_cart_list[$data['store_id']]['goods_list'][] = $data;
                }
                foreach ($store_cart_list as $cart){
                    $carts[] = $cart;
                }
                $lists = $carts;
            }

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除购物车
     */
    public function cartDelete(Request $request) {
        try{
            $user_id = auth()->guard('web')->id();
            if (empty($user_id)) return response()->json($data = ["code"=>300,"data"=>null,"msg"=>'请先登录'], $status = 307);
            $cart_id = $request->post("cart_id");
            if (empty($cart_id)) return $this->writeJson(1,null, '参数错误');
            $ids = explode(",", $cart_id);
            foreach ($ids as $key => $val){
                Carts::where(['id'=>$val,'user_id'=>$user_id])->delete();
            }
            return $this->writeJson(0,null, '操作成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }

    }
}